serverName=main-server
serverVersion=0.1

containerId=`sudo docker ps -a -q --filter name=$serverName`
if [ -n "$containerId" ]; then
    sudo docker kill $containerId
fi

sudo docker build --rm -t $serverName:$serverVersion .

sudo docker run --rm -d --name $serverName \
  --env DB_URL="mongodb://$dbUser:$dbPass@$dbUrl/collection?$dbParams" \
  --env env="test" \
  --publish 8080:8080 \
  --volume main-data:/var/main_home $serverName:$serverVersion

# 前一个main-data在宿主机上/var/lib/docker/volumes/main-data/_data, 后面是映射在虚拟机中的路径

# 删除已结束的容器对应的镜像
imageId=`sudo docker images -f "dangling=true" -q`
if [ -n "$imageId" ]; then
    sudo docker rmi $imageId
fi